package 哈希表;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author Weton Li
 * @Date 2021/4/12 13:21
 */
public class 快乐数 {
    public static boolean isHappy(int n){
        HashSet<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = getNext(n);
        }
        return n == 1;
    }

    private static int getNext(int n) {

        int sum = 0;
        while (n>0){
            int d = n%10;
            n = n/10;
            sum+=d*d;
        }
        return sum;
    }

    public static void main(String[] args) {
        int n = 19;
        boolean happy = isHappy(n);
        System.out.println(happy);
    }
}
